package com.fcat.springboot.mybatis.mapper.provider;

import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

import java.util.Set;


/**
 * @BelongsProject: fcat-base
 * @BelongsPackage: com.fcat.springboot.mybatis.mapper.provider
 * @Author: xiangfei
 * @CreateTime: 2021-07-07
 */
public class UpdateListMapperProvider extends MapperTemplate {

    public UpdateListMapperProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }
    public String updateListById(MappedStatement ms) {
        Class<?> entityClass = getEntityClass(ms);//实体类对象
        String tableName = tableName(entityClass);//表名
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<foreach collection=\"list\" item=\"item\" separator=\";\">")
                .append(SqlHelper.updateTable(entityClass, tableName))
                .append("<set>");
        String idColumn = null;
        String idColumnHolder = null;
        for (EntityColumn column : EntityHelper.getColumns(entityClass)) {
            //判断是否是逐渐
            if (column.isId()) {
                idColumn = column.getColumn();
                idColumnHolder = column.getColumnHolder("item");
            } else {
                stringBuilder.append("`")
                        .append(column.getColumn())
                        .append("`")
                        .append("=")
                        .append(column.getColumnHolder("item"))
                        .append(",");
            }
        }
        stringBuilder.append("</set>").append("where ").append("`").append(idColumn).append("`").append("=").append(idColumnHolder).append("</foreach>").append("\r\n");
        return stringBuilder.toString();
    }

    public String updateListSelective(MappedStatement ms) {
        Class<?> entityClass = getEntityClass(ms);//实体类对象
        String tableName = tableName(entityClass);//表名
        StringBuilder sql = new StringBuilder();
        sql
                .append("<foreach collection=\"list\" item=\"item\" separator=\";\">")
                .append(SqlHelper.updateTable(entityClass, tableName));
        sql.append("<set>");
        //获取全部列
        Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
        //当某个列有主键策略时，不需要考虑他的属性是否为空，因为如果为空，一定会根据主键策略给他生成一个值
        for (EntityColumn column : columnList) {
            if (!column.isId() && column.isUpdatable()) {
                if (true) {
                    sql.append(SqlHelper.getIfNotNull("item", column, column.getColumnEqualsHolder("item") + ",", isNotEmpty()));
                } else {
                    sql.append(column.getColumnEqualsHolder("item") + ",");
                }
            }
        }
        sql.append("</set>");
        sql.append("<where>");
        //获取全部列
        //当某个列有主键策略时，不需要考虑他的属性是否为空，因为如果为空，一定会根据主键策略给他生成一个值
        for (EntityColumn column : columnList) {
            if(column.isId()){
                sql.append(" AND " + column.getColumnEqualsHolder("item"));
            }
        }
        sql.append("</where>");
        sql.append("</foreach>");
        return sql.toString();
    }

}
